package com.bjpowernode.javaweb.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebListener;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;


@WebServlet("/cookie/generate")
public class GenerateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //创建cookie对象
        Cookie cookie=new Cookie("productid","123456789");
        Cookie cookie2=new Cookie("usernameid","zhansgan");

        //设置ccokie在一小时后失效
//        cookie.setMaxAge(60*60);
        cookie.setMaxAge(-1);
        cookie2.setMaxAge(-2);

        //默认情况下，没有设置path，cookie的关联路径是什么
        cookie.setPath(request.getContextPath());
        cookie2.setPath(request.getContextPath());

        //将cookie响应到浏览器
        response.addCookie(cookie);
        response.addCookie(cookie2);
    }
}
